package com.fpj.sat.vehicle_administrator.util;

import java.awt.Dimension;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.sql.DataSource;
import javax.swing.ImageIcon;

import com.fpj.sat.vehicle_administrator.comun.Constantes;
import com.fpj.sat.vehicle_administrator.main.InitApplication;

public class Util {
	
	private static final DateFormat format1 = new SimpleDateFormat("ddMMyyyyhhmmss");
	
	public static int getAnchoPantalla(Dimension d, double mult){
		return (int)Math.round(d.getWidth() * mult);
	}
	
	public static int getAltoPantalla(Dimension d, double mult){
		return (int)Math.round(d.getHeight() * mult) - (int)Math.round(d.getHeight() * 0.06);
	}
	
	public static int getPorcentaje(double valor, double mult){
		return (int)Math.round(valor * mult);
	}
	
	public static ImageIcon getIcon(String iconName){
		ImageIcon imc = new ImageIcon(Util.class.getClassLoader().getResource(Constantes.IMAGE_PATH + iconName));
		
		return imc;
	}
	
	public static <T> T getBean(Class<T> clasz){
		return InitApplication.CONTEXT.getBean(clasz);
	}

	public static Object getBean(String id){
		return InitApplication.CONTEXT.getBean(id);
	}
	
	public static Connection getConnectionDS(){
		try {
			return ((DataSource)getBean("dataSource")).getConnection();
		} catch (SQLException e) {
			return null;
		}
	}
	public static String getNullValue(String value) {
		if(value == null)
			return null;
		if("".equals(value.trim()))
			return null;
		
		return value.toUpperCase();
	}
	public static String getNotNullValue(String value) {
		if(value == null)
			return "";
		
		return value;
	}

	public static String getDateValue(Timestamp value) {
		return format1.format(value);
	}
}
